package test.sp;

import java.io.*;

/**
 * @author oldliu
 * @since 1.0
 */
public class Market implements Serializable {
    //一商场(有名称，地址等信息)的会员系统中，有多个会员，用对象数组表示，假设为100个
    //会员属性有会员号，姓名，手机号，积分，

    String name;
    String address;
    Vip[] vips = new Vip[100];//存放会员
    int count = 0;
    private Liping[]lipings=new Liping[5];
    {
        lipings[0]=new Canjingzhi();
        lipings[1]=new Canjingzhi();
        lipings[2]=new Canjingzhi();
        lipings[3]=new Guahudao();
        lipings[4]=new Chaoguo();
    }
    public Market(){
//        lipings[0]=new Canjingzhi();
//        lipings[1]=new Canjingzhi();
//        lipings[2]=new Canjingzhi();
//        lipings[3]=new Guahudao();
//        lipings[4]=new Chaoguo();


    }
    //商场信息
    public void show(){
        System.out.println("这个商场位于"+address+",名字叫"+name);
        for (int i = 0; i < count; i++) {
            vips[i].show();
        }
        System.out.println("----------------------------------");
    }

    //  1.判断卡号是否重复
    public boolean checkRepeat(String newcard) {
        for (int i = 0; i < count; i++) {
            if (vips[i].getCard().equals(newcard)) {
                return false;//false代表重复
            }
        }
        return true;
    }

    //  2.加人[卡号是否重复，数组是否有空位]
    public boolean add(Vip newvip) {
        ////没有位置和重复
        if (!checkRepeat(newvip.getCard())|| count >= vips.length) {
            System.out.println("无法加人");
            return false;//卡号重复
        }
        //        for (int i = 0; i < vips.length; i++) {
        //            if (vips[i] == null) {
        //                vips[i]=newvip;
        //                return true;
        //            }
        //        }
        vips[count++] = newvip;
        return true;//加人成功
    }
    //加新客户
    public boolean add(String cardno,String name,int score,String phone) {
        if (!checkRepeat(cardno)|| count >= vips.length) {
            System.out.println("无法加人");
            return false;//卡号重复
        }
        vips[count++] = new Vip(name,cardno,score,phone);
        return true;//加人成功
    }

    //  4.加分方法
    public boolean addScore(String cardnum, int score1) {
        for (int i = 0; i < count; i++) {
            if (vips[i].getCard().equals(cardnum)) {
                vips[i].setScore(vips[i].getScore() + score1);
                return true;//代表加分成功
            }
        }
        return false;
    }

    /* 5.兑换礼品   100分兑换餐巾纸；400分兑换剃须刀；1000分兑换炒锅；
        用户输入卡号和需要兑换的奖品，兑换成功后扣除相应积分(礼品类不需要定义，用String返回即可)；
        如果帐号不存在，或者积分不够，兑换失败(提示:如果兑换成功,需要扣除相应的积分)。如果不成功返回null。
        public String duihuan(String cardNo,int lipingleixing)*/
    //public Liping exhange(String cardnum,int choice)
    public Liping exchange(String cardnum, int choice) {
        for (int i = 0; i < count; i++) {
            if (vips[i].getCard().equals(cardnum)) {
                if (choice == 3) {
                    if (vips[i].getScore() >= 1000) {
                        //炒锅
                        for (int j = 0; j < lipings.length; j++) {
                            if(lipings[j]==null)
                                continue;

                            if(lipings[j] instanceof Chaoguo){
                                Liping temp=lipings[j];
                                lipings[j]=null;
                                //移动
                                for (int k = j; k < lipings.length-1; k++) {
                                    lipings[k]=lipings[k+1];
                                }
                                lipings[lipings.length-1]=null;
                                /////
                                vips[i].setScore(vips[i].getScore()-1000);
                                return temp;
                            }
                        }
                    }
                } else if (choice == 2) {
                    if (vips[i].getScore() >= 400) {
                        for (int j = 0; j < lipings.length; j++) {
                            if(lipings[j]==null)
                                continue;

                            if(lipings[j] instanceof Guahudao){
                                Liping temp=lipings[j];
                                lipings[j]=null;
                                vips[i].setScore(vips[i].getScore()-400);
                                return temp;
                            }
                        }
                    }
                } else if (choice == 1) {
                    if (vips[i].getScore() >= 100) {
                        vips[i].setScore(vips[i].getScore() - 100);
                        return null;//?
                    }
                }

                return null;
            }
        }
        return null;
    }
//    public String exchange(String cardnum, int choice) {
//        for (int i = 0; i < count; i++) {
//            if (vips[i].getCard().equals(cardnum)) {
//                if (choice == 3) {
//                    if (vips[i].getScore() >= 1000) {
//                        vips[i].setScore(vips[i].getScore() - 1000);
//                        return "炒锅";
//                    }
//                } else if (choice == 2) {
//                    if (vips[i].getScore() >= 400) {
//                        vips[i].setScore(vips[i].getScore() - 400);
//                        return "剃须刀";
//                    }
//                } else if (choice == 1) {
//                    if (vips[i].getScore() >= 100) {
//                        vips[i].setScore(vips[i].getScore() - 100);
//                        return "餐巾纸";
//                    }
//                }
//            }
//        }
//        return null;
//    }

    public static final String MAR_WEIZH="market.txt";
    public static Market read(){
        try {
            ObjectInputStream oin=new ObjectInputStream(new FileInputStream(MAR_WEIZH));
            Object obj=oin.readObject();
            oin.close();
            return (Market) obj;
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return null;
    }
    public static void store(Market m){
        ObjectOutputStream oout= null;
        try {
            oout = new ObjectOutputStream(new FileOutputStream(MAR_WEIZH));
            oout.writeObject(m);
            oout.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}